<template>
  <div class="warpper">
    <div class="head">
      <div class="user-total item">
        <div class="left">
          <div>用户总量</div>
          <i class="iconfont icon-yonghu1"></i>
        </div>
        <div class="right">
          <div class="number-text">{{ statisticalVal.userTotal }}</div>
        </div>
      </div>
      <div class="new-user-total item">
        <div class="left">
          <div>本周新增用户</div>
          <i class="iconfont icon-xinzengyonghu"></i>
        </div>
        <div class="right">
          <div class="number-text">{{ statisticalVal.newUserTotal }}</div>
        </div>
      </div>
      <div class="activity-total item">
        <div class="left">
          <div>活动总量</div>
          <i class="iconfont icon-huodong-copy"></i>
        </div>
        <div class="right">
          <div class="number-text">{{ statisticalVal.activityTotal }}</div>
        </div>
      </div>
      <div class="new-activity-total item">
        <div class="left">
          <div>本周新增活动</div>
          <i class="iconfont icon-xinzenghuodong"></i>
        </div>
        <div class="right">
          <div class="number-text">{{ statisticalVal.newActivityTotal }}</div>
        </div>
      </div>
    </div>
    <div class="container">
      <div id="main"></div>
      <!-- <div id="right"></div> -->
    </div>
  </div>
</template>

<script>
import { statisticsData, statisticsMonthData } from '@/api/statisticsApi.js'
import * as echarts from 'echarts'
import { GridComponent } from 'echarts/components'
import { BarChart } from 'echarts/charts'
import { CanvasRenderer } from 'echarts/renderers'
echarts.use([GridComponent, BarChart, CanvasRenderer])
export default {
  data() {
    return {
      statisticalVal: {},
    }
  },
  created() {
    this.getData()
  },
  mounted() {
    statisticsMonthData().then((res) => {
      let data = res.data.data
      let formatData = [['product', '新增活动', '新增用户']]
      for (var i = 1; i < 13; i++) {
        let arr = [i + '月', data[0][i], data[1][i]]
        formatData[i] = arr
      }
      this.initEchart(formatData)
    })
  },
  methods: {
    getData() {
      statisticsData().then((res) => {
        this.statisticalVal = res.data.data
      })
    },
    initEchart(data) {
      var chartDom = document.getElementById('main')
      var myChart = echarts.init(chartDom)
      var option

      option = {
        legend: {
          top: 50,
        },
        tooltip: {},
        title: {
          text: '本年度月统计数据',
          top: 50,
          left: 100,
        },
        grid: {
          top: 100,
        },
        dataset: {
          source: data,
        },
        xAxis: { type: 'category' },
        yAxis: {},
        series: [{ type: 'bar' }, { type: 'bar' }],
      }
      option && myChart.setOption(option)
      window.addEventListener('resize', function () {
        myChart.resize()
      })
    },
  },
}
</script>

<style lang="less" scoped>
.warpper {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .head {
    width: 100%;
    // height: 170px;
    flex: 1.5;
    border: 3px dashed #ebeef5;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .user-total {
      background: #69d6ff;
    }
    .new-user-total {
      background: #ffd56b;
    }
    .activity-total {
      background: #ff9ccb;
    }
    .new-activity-total {
      background: #b0abff;
    }
    .item {
      width: 250px;
      height: 80px;
      border-radius: 10px;
      color: #fff;
      padding: 10px;
      display: flex;
      .left {
        flex: 1.3;
        height: 100%;
        position: relative;
        i {
          font-size: 46px;
          position: absolute;
          bottom: 0;
        }
      }
      .right {
        flex: 2;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        .number-text {
          font-size: 36px;
        }
      }
    }
  }
  .container {
    width: 100%;
    flex: 5;
    display: flex;

    #main {
      flex: 1;
    }
  }
}
</style>